package com.ddyx.util.template;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.ddyx.util.PropertiesUtil;
import com.ddyx.vo.TempMsgResp;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @ClassName: TemplateMessageUtil.java
 * @Description: 此接口只用于给微信端发送服务消息通知
 * @author: lhj
 * @date: 2018年1月2日
 */
public class TemplateMessageUtil {
//	private static CloseableHttpClient httpClient = HttpClients.createDefault();
	private static final String appId = PropertiesUtil.getProperty("sys.appid");
	private static final String appSecret = PropertiesUtil.getProperty("sys.appSecret");
	private static ObjectMapper jsonMapper = new ObjectMapper();
	/**
	 * @description 发送模版消息,用户主动操作时触发的消息通知
	 * @param formId
	 * @param openId
	 */
	public static void sendTmpMsg(String json) {
		String token = getToken();
		if (StringUtils.isNotBlank(json)) {
			String url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + token;
			HttpPost httpPost = new HttpPost(url);
			try {
				StringEntity reqEntity = new StringEntity(json,"utf-8");
				httpPost.setEntity(reqEntity);
				CloseableHttpResponse response = HttpClients.createDefault().execute(httpPost);
				HttpEntity respEntity = response.getEntity();
//				response.close();//关闭 接收
				String respEntityStr = EntityUtils.toString(respEntity);
				TempMsgResp tempMsgResp = jsonMapper.readValue(respEntityStr, TempMsgResp.class);//获取发送后的返回值
			}catch (Exception e) {
				e.printStackTrace();
			}

		} else {
			System.out.println("参数有误！！！！！！！");
		}
	}


	/**
	 * @description 获取access_token
	 * @return token
	 * @throws Exception
	 */
	public static String getToken() {
		String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret="
				+ appSecret + "";
		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = null;
		HttpEntity entity = null;
		String token = null;
		try {
			response = HttpClients.createDefault().execute(httpGet);
			entity = response.getEntity();
			String json = EntityUtils.toString(entity, "utf-8");
//			System.out.println(json);
			Map map = jsonMapper.readValue(json, Map.class);
			token = (String) map.get("access_token");
//			response.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return token;
	}
	
	
//	public static void orderPayNoticeHandler(String goodsName,BigDecimal orderPrice,String contactName,String contactPhone,String address,String orderId,String storeNo) {
//		
//	}
	
	
}
